package com.tanhua.dubbo.api.impl;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.crypto.SecureUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.tanhua.dubbo.api.UserApi;
import com.tanhua.dubbo.mapper.UserMapper;
import com.tanhua.dubbo.pojo.User;
import com.tanhua.dubbo.pojo.UserInfo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;

@DubboService(version = "1.0.0") //声明这是一个dubbo服务
public class UserApiImpl implements UserApi {

    @Autowired
    private UserMapper userMapper;



    @Override
    public User queryByMobile(String mobile) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("mobile", mobile); //查询条件
        return this.userMapper.selectOne(queryWrapper);
    }

    @Override
    public User queryById(Long id) {
        return this.userMapper.selectById(id);
    }

    @Override
    public Long save(String mobile) {
        User user = new User();
        user.setMobile(mobile);
        user.setPassword(SecureUtil.md5("123456")); //默认密码：123456

        this.userMapper.insert(user);
        return user.getId(); //主键回填
    }

    /*
     *更新手机号
     * */
    @Override
    public Object update(User user) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", user.getId());
        return this.userMapper.update(user, queryWrapper) > 0;

    }

    @Override
    public Boolean queryUserIdByMobile(String mobile) {

        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("mobile", mobile);
        User user = this.userMapper.selectOne(queryWrapper);
        if (ObjectUtil.isEmpty(user)) {
            return true;
        }
        return false;
    }
}
